package com.mys.maggie.redis.api.util;


import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

/**
 * @Description: jackson工具类
 * @Date: Created in 2019/3/6 17:12
 * @Modified By:
 */
public class JackSonUtil {

    private static final ObjectMapper defaultMapper = new ObjectMapper();

    static {
        //defaultMapper.disable(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS);

        //序列化的时候序列对象的所有属性
        defaultMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        // 忽略json字符串中不识别的属性
        defaultMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        // 忽略无法转换的对象 “No serializer found for class com.xxx.xxx”
        defaultMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        defaultMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        defaultMapper.configure(SerializationFeature.FAIL_ON_SELF_REFERENCES, false);
    }

    public static <T> T parseObjectThrowException(ObjectMapper mapper, String json, TypeReference<T> type) throws IOException {
        if (StringUtils.isEmpty(json)) {
            return null;
        }
        if (mapper == null) {
            mapper = defaultMapper;
        }
        return mapper.readValue(json, type);
    }

    public static <T> T parseObject(ObjectMapper mapper, String json, TypeReference<T> type) {
        try {
            return parseObjectThrowException(mapper, json, type);
        } catch (IOException e) {
        }
        return null;
    }

    public static <T> T parseObjectThrowException(ObjectMapper mapper, String json, Class<T> clazz) throws IOException {
        if (StringUtils.isEmpty(json)) {
            return null;
        }
        if (mapper == null) {
            mapper = defaultMapper;
        }
        return mapper.readValue(json, clazz);
    }

    public static <T> T parseObject(ObjectMapper mapper, String json, Class<T> clazz) {
        try {
            return parseObjectThrowException(mapper, json, clazz);
        } catch (IOException e) {
        }
        return null;
    }

    public static String toJSONString(ObjectMapper mapper, Object object) {
        try {
            if (mapper == null) {
                mapper = defaultMapper;
            }
            return mapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {

        }
        return "";
    }

    public static <T> T parseObject(String json, Class<T> clazz) {
        return parseObject(defaultMapper, json, clazz);
    }

    public static String toJSONString(Object object) {
        return toJSONString(defaultMapper, object);
    }
}
